#!/data/data/com.termux/files/usr/bin/bash
#
# Add To Menu
# A simple script to add programs from the installed proot-distro
# to the Termux application menu.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author     : @sabamdarif
# License    : GPL-v3
# Description: Adds programs from a proot-distro to the Termux application menu
# Part of    : sabamdarif/termux-desktop
# Repository : https://github.com/sabamdarif/termux-desktop

# Set distro_name from environment variable or default to "debian"
distro_name="${distro_name:-debian}"
distro_path="/data/data/com.termux/files/usr/var/lib/proot-distro/installed-rootfs/${distro_name}"

# Function to list displayable .desktop files
list_desktop_files_with_name() {
    find "$1" -name "*.desktop" -type f -exec awk -F '=' '
    /^Name=/ {name=$2}
    /^NoDisplay=/ {no_display=$2}
    END {
        if (no_display != "true") {
            gsub(/.desktop$/, "", name);
            print name ";;" FILENAME
        }
    }' {} \;
}

# Function to handle adding and removing applications
add2menu_f() {
    # Prompt user to choose an action
    action=$(zenity --list --title="Choose Action" --text="Select an action:" --radiolist --column="Select" --column="Action" \
        TRUE "Add Application To Termux Menu" FALSE "Remove Application From Termux Menu")

    # Exit if no action is selected
    if [[ -z $action ]]; then
        zenity --info --text="No action selected. Quitting..." --title="Operation Cancelled"
        exit 0
    fi

    # Add applications
    if [[ $action == "Add Application To Termux Menu" ]]; then
        # Fetch list of .desktop files
        files=$(list_desktop_files_with_name "$distro_path/usr/share/applications/")

        # Check if any applications are found
        if [[ -z $files ]]; then
            zenity --info --text="No applications found to add. Quitting..." --title="Operation Cancelled"
            exit 0
        fi

        # Display applications and get user selection
        selected_names=$(zenity --list --title="Add To Termux Menu" --text="Select Applications to Add:" \
            --checklist --column="Include" --column="Name" --separator="," $files)

        # Exit if no applications are selected
        if [[ -z $selected_names ]]; then
            zenity --info --text="No applications selected. Quitting..." --title="Operation Cancelled"
            exit 0
        fi

        # Process each selected application
        IFS=',' read -ra selected_names_array <<<"$selected_names"
        for selected_item in "${selected_names_array[@]}"; do
            selected_name=${selected_item%%;;*}
            selected_file=${selected_item##*;;}

            # Copy and modify the .desktop file
            cp "$selected_file" "$PREFIX/share/applications/pd_added/"
            sed -i 's/^Exec=/Exec=pdrun /' "$PREFIX/share/applications/pd_added/${selected_file##*/}"
        done

        # Update desktop database and icon cache
        update-desktop-database "$PREFIX/share/applications/"
        gtk-update-icon-cache "$PREFIX/share/icons/hicolor"

        zenity --info --text="Selected applications added successfully!" --title="Success"

    # Remove applications
    elif [[ $action == "Remove Application From Termux Menu" ]]; then
        # List existing applications in Termux menu
        files=$(list_desktop_files_with_name "$PREFIX/share/applications/pd_added/")

        # Check if any applications are found
        if [[ -z $files ]]; then
            zenity --info --text="No applications found to remove. Quitting..." --title="Operation Cancelled"
            exit 0
        fi

        # Display applications for removal
        selected_names=$(zenity --list --title="Remove From Termux Menu" --text="Select Applications to Remove:" \
            --checklist --column="Include" --column="Name" --separator="," $files)

        # Exit if no applications are selected
        if [[ -z $selected_names ]]; then
            zenity --info --text="No applications selected. Quitting..." --title="Operation Cancelled"
            exit 0
        fi

        # Process each selected application for removal
        IFS=',' read -ra selected_names_array <<<"$selected_names"
        for selected_item in "${selected_names_array[@]}"; do
            selected_file=${selected_item##*;;}
            rm "$selected_file"
        done

        # Update desktop database and icon cache
        update-desktop-database "$PREFIX/share/applications/"
        gtk-update-icon-cache "$PREFIX/share/icons/hicolor"

        zenity --info --text="Selected applications removed successfully!" --title="Success"
    fi
}

# Handle script arguments
case $1 in
--nogpu)
    LIBGL_ALWAYS_SOFTWARE=1 MESA_LOADER_DRIVER_OVERRIDE=llvmpipe GALLIUM_DRIVER=llvmpipe add2menu_f
    ;;
--help | -h)
    echo -e "
Usage:
  add2menu_legacy             Start add2menu with GPU acceleration
  add2menu_legacy --nogpu     Start add2menu without GPU acceleration
  env distro_name=fedora ./add2menu/add2menu_legacy  Run for a specific distro
"
    ;;
*)
    add2menu_f
    ;;
esac

